package com.emmmya.ocs.modules.system.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.emmmya.ocs.common.base.BaseDao;
import com.emmmya.ocs.modules.system.dao.TimedTaskDao;
import com.emmmya.ocs.modules.system.dao.mapper.TimedTaskMapper;
import com.emmmya.ocs.modules.system.entity.TimedTask;
import com.emmmya.ocs.modules.system.manager.QuartzManager;
import com.emmmya.ocs.modules.system.service.TimedTaskService;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 定时任务调度业务对象
 *
 * @author xm
 * @date	2019-12-2 14:50:59
 */
@Service
public class TimedTaskServiceImp implements TimedTaskService {
 	@Autowired
	TimedTaskDao timedTaskDao;


	@Autowired
	private TimedTaskMapper timedTaskMapper;

	@Autowired
	QuartzManager quartzManager;


	@Override
	public BaseDao<TimedTask, String> getRepository() {
		return timedTaskDao;
	}

	@Override
	public List<TimedTask> getTimedTask(TimedTask bit) {
		return timedTaskMapper.getTimedTask(bit);
	}
 
	@Override
	public boolean updateTimedTask(TimedTask bit) {
		try {
			timedTaskMapper.updateById(bit);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

	@Override
	public void getListByPage(IPage<TimedTask> page, Wrapper<TimedTask> wrapper) {
		timedTaskMapper.selectPage(page,wrapper);
	}
 
	@Override
	public String addTimedTask(TimedTask bit) {
		String id = UUID.randomUUID().toString();
		try {
			bit.setCreateTime(new Date());
			bit.setJobId(id);
			//bit.setStatus("0");
			TimedTask save = timedTaskDao.save(bit);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return id;
	}


	@Override
	public void delTimedTask(String bitIds) {
		try {
			timedTaskDao.deleteById(bitIds);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}



	@Override
	public TimedTask getByKey(String id) {
		return timedTaskDao.getOne(id);
	}


	@Override
	public void run(TimedTask dingshi) throws SchedulerException {
		quartzManager.runJobNow(dingshi);
	}

	/**
	 * 把数据的定时任务数据转成系统的任务
	 * @throws SchedulerException
	 */
	@Override
	public void initSchedule() throws SchedulerException {
		// 这里获取任务信息数据
		List<TimedTask> timedTasks = this.getTimedTask(new TimedTask());
		for (TimedTask timedTask : timedTasks) {
			if("1".equals(timedTask.getStatus())){
				quartzManager.addJob(timedTask);
			}
		}
 
	}
 
	@Override
	public void addJob(String id) {
		TimedTask byKey = this.getByKey(id);
		quartzManager.addJob(byKey);
	}
 
	@Override
	public void pauseJob(String id) {
		TimedTask byKey = this.getByKey(id);
		try {
			quartzManager.pauseJob(byKey);
			byKey.setStatus("0");
			update(byKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	@Override
	public void resumeJob(String id) {
		TimedTask byKey = this.getByKey(id);
		try {
			if(quartzManager.isExist(byKey)) { //通用部分
				quartzManager.deleteJob(byKey);
			}
			quartzManager.addJob(byKey);
			byKey.setStatus("1");
			update(byKey);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
 
	@Override
	public void deleteJob(String id) {
		TimedTask byKey = this.getByKey(id);
		try {
			quartzManager.deleteJob(byKey);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
 
	@Override
	public void runJobNow(String id) {
		TimedTask byKey = this.getByKey(id);
		try {
			quartzManager.runJobNow(byKey);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void updateJobCron(TimedTask dingshi) {
		try {
			quartzManager.updateJobCron(dingshi);
		} catch (SchedulerException e) {
			e.printStackTrace();
		}
	}
 
	@Override
	public List<TimedTask> getAllJob() {
		try {
			return quartzManager.getAllJob();
		} catch (SchedulerException e) {
			e.printStackTrace();
			return null;
		}
	}
 
	@Override
	public List<TimedTask> getRunningJob() {
		try {
			return quartzManager.getRunningJob();
		} catch (SchedulerException e) {
			e.printStackTrace();
			return null;
		}
	}

}